package com.malguy.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author malguy-wang sir
 * @create ---
 */
@Controller
public class fileController {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    //文件上传
    @PostMapping("/upload")
    @ResponseBody
    public String Update(MultipartFile file, HttpServletRequest req) {
        //准备文件夹
        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/") + format;
        File folder = new File(realPath);
        if (!folder.exists()) {
            //如果路径不存在就创建
            folder.mkdirs();
        }
        //准备文件名
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        //存储
        try {
            file.transferTo(new File(folder, newName));
            //组装url
            //getScheme获取请求协议
            String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }
    @PostMapping("/upload2")
    @ResponseBody
    public String Update2(@RequestParam("file")MultipartFile[] files, HttpServletRequest req) {
        //准备文件夹
        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/") + format;
        File folder = new File(realPath);
        if (!folder.exists()) {
            //如果路径不存在就创建
            folder.mkdirs();
        }
        try {
            for (MultipartFile file : files) {
                //准备文件名
                String oldName = file.getOriginalFilename();
                String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
                //存储
                file.transferTo(new File(folder, newName));
                //组装url
                //getScheme获取请求协议
                String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/" + format + newName;
                System.out.println(url);
            }
            return "OK";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "done";
    }

    @PostMapping("/upload3")
    @ResponseBody
    public String Update3(MultipartFile file1, MultipartFile file2, HttpServletRequest req) {
        //准备文件夹
        String format = sdf.format(new Date());
        String realPath = req.getServletContext().getRealPath("/") + format;
        File folder = new File(realPath);
        if (!folder.exists()) {
            //如果路径不存在就创建
            folder.mkdirs();
        }
        try {
            System.out.println(getUrl(file1, folder, req, format));
            //准备文件名
            String oldName1 = file2.getOriginalFilename();
            String newName1 = UUID.randomUUID().toString() + oldName1.substring(oldName1.lastIndexOf("."));
            //存储
            file2.transferTo(new File(folder, newName1));
            //组装url
            //getScheme获取请求协议
            String url1 = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/" + format + newName1;
            System.out.println(url1);
            return "OK";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "done";
    }

    public String getUrl(MultipartFile file1, File folder, HttpServletRequest req, String format) {
        //准备文件名
        String oldName = file1.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        //存储
        try {
            file1.transferTo(new File(folder, newName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //组装url
        //getScheme获取请求协议
        String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/" + format + newName;
        return url;
    }
}
